/**
 * 
 */
package com.alkandros.myloke.activities;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.alkandros.myloke.R;
import com.alkandros.myloke.adapters.PlacesAdapter;
import com.alkandros.myloke.constants.IAppConstants;
import com.alkandros.myloke.managers.ApiManager;
import com.alkandros.myloke.managers.ApiManager.ApiResponseListner;
import com.alkandros.myloke.managers.AppPreferenceManager;
import com.alkandros.myloke.models.FaithModel;
import com.alkandros.myloke.models.NearByModel;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;

/**
 * @author Jiju Induchoodan
 * 
 */
public class SplashActivity extends Activity implements IAppConstants {

	private Context context;
	private Intent i;
	protected ApiManager apiManager;
	private ArrayList<FaithModel> faithModelArray;

	private final int SPLASH_DISPLAY_LENGTH = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		context = this;
		/* if(apiManager==null) */
		
		
		if (AppPreferenceManager.getUserName(context).equals(""))
		{
			fetchFaiths();
		}
		else
		{
			i = new Intent(context, NearbyActivity.class);
			startActivity(i);
			finish();
		}
		
//		intializeUI();
	}

	private void fetchFaiths()
	{

		apiManager = new ApiManager(FAITH_SERVICE, context, true);
		apiManager.setApiResponseListener(new ApiResponseListner() {

			@Override
			public void dataDownloadedSuccessfully(JSONObject response) {
				// TODO Auto-generated method stub

				faithModelArray = new ArrayList<FaithModel>();
				FaithModel model = new FaithModel();
				try {
					JSONArray faith = response.getJSONArray(FAITH);
					for (int i = 0; i < faith.length(); i++) {
						JSONObject jitem = faith.getJSONObject(i);
						model = new Gson().fromJson(jitem.toString(),
								FaithModel.class);

						faithModelArray.add(model);
					}
					
					AppPreferenceManager.saveFaithList(context, faithModelArray);
					if (AppPreferenceManager.getUserName(context).equals("")) {
						i = new Intent(context, MainActivity.class);
						i.putExtra("faithArray", faithModelArray);
					} else {
						i = new Intent(context, NearbyActivity.class);
					}
					startActivity(i);
					finish();
					Log.e("", "Array size" + faithModelArray.size());

				} catch (Exception e) {
					Log.e("", "Parsing error" + e.getMessage());
				}

			}

			@Override
			public void dataDownloadedFailed(String error) {
				// TODO Auto-generated method stub

			}
		});
	}
	
	private void intializeUI() {
		new Handler().postDelayed(new Runnable() {

			public void run() {

				if (AppPreferenceManager.getUserName(context).equals("")) {
					i = new Intent(context, MainActivity.class);
				} else {
					i = new Intent(context, NearbyActivity.class);
				}
				startActivity(i);
				finish();
			}
		}, SPLASH_DISPLAY_LENGTH);
	}
}